import Vue from 'vue'
import VueRouter from 'vue-router'
import axios from 'axios'
// import NewInfo from '../views/Home/NewInfo'
const NewInfo = () => import('../views/Home/NewInfo')
// import Winfo from '../views/Watermelon/WInfo'
const Winfo = () => import('../views/Watermelon/WInfo')
// import Login from '../views/User/login'
const Login = () => import('../views/User/login')
// import Register from '../views/User/register'
const Register = () => import('../views/User/register')
// import TakeCare from '../views/User/takecare'
const TakeCare = () => import('../views/User/takecare')
// import Message from '../views/User/message'
const Message = () => import('../views/User/message')
// import Collect from '../views/User/collect'
const Collect = () => import('../views/User/collect')
const Watermelon = () => import('../views/Watermelon')
// import Watermelon from '../views/Watermelon'
// import Fire from '../views/Fire'
const Fire = () => import('../views/Fire')
// import User from '../views/User'
const User = () => import('../views/User')
// import Home from '../views/Home'
const Home = () => import('../views/Home')
Vue.use(VueRouter)

axios.defaults.baseURL = 'http://192.168.157.155:8082'

Vue.prototype.$http = axios

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    component: Home
  },
  {
    path: '/winfo/:id',
    component: Winfo
  },
  {
    path: '/home/newinfo/:id',
    component: NewInfo
  },
  {
    path: '/watermelon',
    component: Watermelon
  },
  {
    path: '/fire',
    component: Fire
  },
  {
    path: '/user',
    component: User
  }, {
    path: '/user/login',
    component: Login
  }, {
    path: '/user/register',
    component: Register
  }, {
    path: '/user/takecare',
    component: TakeCare
  }, {
    path: '/user/message',
    component: Message
  }, {
    path: '/user/collect',
    component: Collect
  }
]

Vue.prototype.getFormate = function (reltime) {
  if (reltime === undefined) {
    return
  }
  let newtime = new Date().valueOf()
  reltime = new Date(reltime.substring(0, 19).replace(/-/g, '/')).getTime()
  const milliseconds = newtime - reltime
  let timeStr
  if (milliseconds <= 1000 * 60 * 1) {
    timeStr = '刚刚'
  } else if (
    milliseconds >= 1000 * 60 * 1 &&
    milliseconds <= 1000 * 60 * 60
  ) {
    timeStr = Math.round(milliseconds / (1000 * 60)) + '分钟前'
  } else if (
    milliseconds >= 1000 * 60 * 60 * 1 &&
    milliseconds <= 1000 * 60 * 60 * 24
  ) {
    timeStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前'
  } else if (
    milliseconds >= 1000 * 60 * 60 * 24 &&
    milliseconds <= 1000 * 60 * 60 * 24 * 30
  ) {
    timeStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'
  } else {
    timeStr = '30天前'
  }
  return timeStr
}

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/user' || to.path === '/home' || to.path === '/watermelon' || to.path === '/fire' || to.path === '/user/login' || to.path === '/user/register') return next()
  const token = localStorage.getItem('users')
  if (!token) {
    alert('请先登录')
    next('/user/login')
  }
  next()
})

export default router
